(function ($) {
	var sourceData;
	var perInfo,pageType,nurseid,perid
	var domArr = []
	var unUploadFiles=[] //未上传的图片
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//注册资格 date|2022-08-01^id|23^PerDR|1^flag|N
	//离职 ResignWard|1^ResignName|1393^ResignDate|^ResignReason|20__1~20__3~20__5^RowID|13^ResignType|N^ResignCreater|1^ResignStatus|N^ResignOther|
	function submitNewData(){
		blurAllInput()
		var parr = ''
		var status = this.getAttribute('data-id');
		var method ='web.INMPerSubComm:SaveRegisted'
		var rw = ''
		if (pageType == '离职备案') {
			method = 'web.INMInternComm:SaveResign'
			var reason = $('#ResignReason')[0].getAttribute('data-id')
			if (reason=="") {
				mui.toast('请选择离职原因')
				return
			}
			if (sourceData) {
				rw = sourceData['rowid']
			}
			parr = 'ResignWard|'+perInfo['loginWardId']+'^ResignName|'+perid+'^ResignDate|^ResignReason|'+reason+'^RowID|'+rw+
				'^ResignType|N^ResignCreater|'+nurseid+'^ResignStatus|'+status+'^ResignOther|'
		}else{
			var date = $('#RegistedDate')[0].innerText
			if (date == '') {
				mui.toast('请选择注册日期')
				return
			}
			if (sourceData) {
				rw = sourceData['RowID']
			}
			parr = 'date|'+date+'^id|'+rw+'^PerDR|'+perid+'^flag|'+status
		}
		var pdict = {'parr':parr}
		//console.log(parr)
		var paramDict = {'method':method,'params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			console.log(JSON.stringify(result))
			var msgcode =result['msgcode']
			if (msgcode == '-1') {
				mui.toast(result['msg'])
				return 
			}
			
			if (unUploadFiles.length==0 || pageType == '离职备案') {
				plus.webview.getWebviewById('../WKPlatformPages/apply/applyList.html').evalJS('detailChanged()')
				mui.back()
				return;
			}
			var path = 'registed'
			var method = 'web.INMPersonComm:InsertPic'
			var paramDict={'id':perid,'path':path,'imgArr':unUploadFiles,'method':method,'otherParams':result['msg']+'||3||','spStr':'^'}
			saveFileAction(paramDict,function(flag){
				if (!flag) {
					return
				}
				unUploadFiles=[]
				plus.webview.getWebviewById('../WKPlatformPages/apply/applyList.html').evalJS('detailChanged()')
				mui.back()
			})
		}, function(errorStr) {
			mui.toast(errorStr)
		});
	}
	
	function getTransDetail(){
		var pdict = {'id':sourceData['RowID']};
		var method = 'web.INMPerSubComm:GetRegistedItem'
		if (pageType == '离职备案') {
			pdict = {'id':sourceData['rowid']};
			method = "web.INMInternComm:GetResign"
		}
		var paramDict = {'method':method,'params':JSON.stringify(pdict),'type':'RecMethod'}
		$.DHCWebService(paramDict, function(result) {
			console.log(JSON.stringify(result))
			var dataDict = result.data
			updateUI(dataDict)
		}, function(errorStr) {
		 	
		});
	}
	
	
	function updateUI(dataDict){
		if (pageType != '离职备案') {
			$('#RegistedDate')[0].innerText = dataDict['RegistedDate']
			updateImageFileUI('registerFile','fileUL',dataDict,dataDict['RowID'])
			return 
		}
		var ResignReason = $('#ResignReason')[0]
		ResignReason.innerText = dataDict['ResignReasonDesc']
		ResignReason.setAttribute('data-id',dataDict['ResignReason'])
		
	}

	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		mui.previewImage();
		var self = plus.webview.currentWebview()
		sourceData = self.data
		pageType = self.pageType
		perInfo = self.perInfo
		nurseid = perInfo['loginID']
		perid = perInfo['loginPerID']
		if (sourceData) { //修改
			getTransDetail()
		}
		$('#CreatorName')[0].innerText = perInfo['loginName']
		$('#applyLoc')[0].innerText = perInfo['loginWardDesc']

		//关闭按钮
		mui('body').on('tap','#closeBtn',function(){
			blurAllInput()
			mui.back()
		})
		//保存提交按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		// 日期
		mui('#UL').on('tap','#RegistedDate',selectDate);
		mui('#UL').on('tap','#ResignDate',function(){
			mui.toast('离职日期为审核通过后的日期')
		});
		
		mui('#UL').on('tap','#ResignReason',openResignReasonPage);
		window.addEventListener('resize',windowResize)
		if (pageType == '离职备案') {
			document.body.classList.add('Resign')
			return
		}
		document.body.classList.add('Registed')
		$('#addFileBtn').on('change','input',function(){
			var imgArr = this.files
			var name = imgArr[0]['name']
			if (fileTypeIcon(name) != 'img') {
				mui.toast('仅支持图片哦~~')
				return;
			}
			unUploadFiles = afterSelectImageFile('fileUL',unUploadFiles,imgArr)
		});
		//监听文件删除按钮
		mui('#fileUL').on('tap','.mui-icon-closeempty',function(){
			var method = 'web.INMPersonComm:DeleteCert'
			var paramDict={'imgArr':unUploadFiles,'method':method,'nurseid':nurseid,'otherParams':'||3||'}
			deleteFileBtnTapped(this,paramDict,function(flag,arr){
				if (arr) {
					unUploadFiles = arr
				}
			});
		}) 
		mui.back = function(){
			if (unUploadFiles.length > 0) {
				mui.confirm('您有未上传的文件','温馨提示',['放弃','继续操作'],function (e) {
					if (e.index == 0) {
						plus.nativeUI.closeWaiting()
						plus.webview.currentWebview().close()
					}
				},'div')
				return
			}
			plus.nativeUI.closeWaiting()
			plus.webview.currentWebview().close()
		}
	}
	function openResignReasonPage(){
		var reasonIds = $('#ResignReason')[0].getAttribute('data-id')
		var prePage = plus.webview.currentWebview()
		var webview = plus.webview.create('resignReason.html', 'resignReason', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{'reasonIds':reasonIds,'prePageId':prePage.id});
		webview.show('fade-in');
	}
})(mui)

function updateResignReason(ids,descs){
	var ResignReason = mui('#ResignReason')[0]
	ResignReason.setAttribute('data-id',ids)
	ResignReason.innerHTML = descs
}

